package com.example.mall.result;

import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.util.HttpStatus;
import com.example.mall.util.StringUtils;
import lombok.Data;

import java.util.HashMap;
import java.util.Objects;

/**
 * 操作消息提醒
 *
*/
@Data
public class JSONResult
{
    private boolean success = true;

    private String message = "成功";

    //错误码，用来描述错误类型 ，1000 表示么有错误
    private String code = "1000";

    //返回的数据
    private Object data;

    /** 创建当前实例 **/
    public static JSONResult success(){
        return new JSONResult();
    }
    /** 创建当前实例 **/
    public static JSONResult success(Object obj){
        JSONResult instance = new JSONResult();
        instance.setData(obj);
        return instance;
    }

    public static JSONResult success(String message,Object obj){
        JSONResult instance = new JSONResult();
        instance.setMessage(message);
        instance.setData(obj);
        return instance;
    }

    public static JSONResult success(Object obj,String code){
        JSONResult instance = new JSONResult();
        instance.setCode(code);
        instance.setData(obj);
        return instance;
    }
    /** 创建当前实例 **/

    public static JSONResult error(String message,String code){
        JSONResult instance = new JSONResult();
        instance.setMessage(message);
        instance.setSuccess(false);
        instance.setCode(code);
        return instance;
    }

    public static JSONResult error(){
        JSONResult jsonResult = new JSONResult();
        jsonResult.setSuccess(false);
        return jsonResult;
    }

    /** 创建当前实例 **/
    public static JSONResult error(String message){
        return error(message,null);
    }

    public static JSONResult error(ErrorEnumCode errorEnumCode) {
        return error(errorEnumCode.getDescription(),String.valueOf(errorEnumCode.getCode()));
    }


    public static JSONResult errorJSR303(String error) {
        return error(error);
    }
}
